/**
 * @created 14-Mar-2006 10:41:20 p.m.
 * @version 0.0.1
 */
 
 import SCUMMCharacter.SCUMMActor;
 import SCUMMCore.SCUMMState;
 
 class SCUMMCharacter.States.SCUMMActorStateTalking implements SCUMMState{
 	//Timer for skip phase proposes
 	private var lastClicksTakenForSkip:Number;
 	//Timers and speeders for text control(talk)
	private var textSpeed:Number;
	//millisenconts per char(included spaces)
	private var lastClicksTaken:Number;
	//For time measure
	private var totalTextDuration:Number;
	//The current text duration un milliseconds(amount of time that the text will be displayed)
	private var arrTextToSay:Array;
	//The array wich uses for store the lines if the actor(his script). Used to Talk functions.
	private var intTextToSayIndex:Number;
 	private var intPriotity:Number;
 	private var oActor:SCUMMActor;
 	
 	private var txtTalk:TextField;
 	
 	function SCUMMActorStateTalking(actor:SCUMMActor){
  		lastClicksTakenForSkip = 0;
 		textSpeed = 150;
 		this.oActor = actor;
 		//priority of the state says if a state can be override by another
		//if the action is uncompleted. The higer the priority, 
		//most unoverrideable is the state
		intPriotity = 2;
		
		// In flash 8 createTextField return a TextField
		this.oActor.mc.createTextField("TalkedText", _root.getNextHighestDepth(), 0, 0, 0, 0);
		this.txtTalk = this.oActor.mc.TalkedText;
		this.txtTalk.multiline = true;
		this.txtTalk.autoSize = true;
		this.txtTalk.textColor = 0xFFFFFF;
 	}
 	public function skipCurrentPhase():Void {
 		//an action cant be skipped junt once each 0.05 secconds
 		if( (getTimer() - lastClicksTakenForSkip) > 50 ){
			passToNextScriptToSay();
	 		lastClicksTakenForSkip = getTimer();
 		}
 		else {
 			this.removeTextField();
			arrTextToSay = null;
			this.oActor.gotoProperIdleFrame();
			this.oActor.state = null;
 		}
 		
 	}
 	
 	public function hasMoreScriptToSay():Boolean {
 		if( (getTimer() - lastClicksTakenForSkip) > 50 )
 			return true;
 		else
 			return false;
 	}
 	/**
	 *@return intPriotity
	 */
 	public function getIntPriotity():Number {
		return intPriotity;
	}
 	
 	public function handle():Void{
  		if (arrTextToSay) {
			//The time for talking ends already?
			if ((getTimer()-lastClicksTaken)>totalTextDuration){
				passToNextScriptToSay();
			} else {
				//If enters here, means the time estimated to talk still not expired
				if (!this.oActor.isMovingLips()) {
					//if is talkinmg we must make the actor move the lips...
					if (this.oActor.isLookingFront()) {
						this.oActor.mc.gotoAndPlay("TalkFront");
					}
					
					if (this.oActor.isLookingBack()) {
						this.oActor.mc.gotoAndPlay("TalkBack");
					}
					if (this.oActor.isLookingLeft() || this.oActor.isLookingRight()) {
						this.oActor.mc.gotoAndPlay("TalkSide");
					}
				}
			}
		}
 	}
 	/***
 	 * Says the strings in the array once a time. 	 */
 	
 	public function setScriptToSay(arrScript:Array, intSpeed:Number):Void{
  		var tempTextSpeed = textSpeed;
		//in case that is necesary override the default text speed;
		if (intSpeed) {
			tempTextSpeed = intSpeed;
		}
 		//Determino si es un array
		intTextToSayIndex = 0;
		arrTextToSay = arrScript;
		intTextToSayIndex = 0;
		arrTextToSay = arrScript;
		totalTextDuration = arrTextToSay[0].length*tempTextSpeed;		
		
		this.setText(arrTextToSay[intTextToSayIndex]);
		lastClicksTaken = getTimer();
	}
 
	public function passToNextScriptToSay()
	{
		var tempTextSpeed = textSpeed;
		//in case that is necesary override the default text speed;
		intTextToSayIndex++;
		
		//Are still scripts to say in the array?
		if (intTextToSayIndex<arrTextToSay.length)
		{
			this.setText(arrTextToSay[intTextToSayIndex]);
			totalTextDuration = arrTextToSay[intTextToSayIndex].length*textSpeed;
			lastClicksTaken = getTimer();
		}
		else
		{
			this.removeTextField();
			arrTextToSay = null;
			this.oActor.gotoProperIdleFrame();
			this.oActor.state = null;
		}
	}
	
	private function removeTextField()
	{
		if ( this.txtTalk != undefined ) {
			this.txtTalk.text = "";
			this.txtTalk.removeTextField();
			this.txtTalk = undefined;
		}
	}
	
	private function setText ( psText:String )
	{
		this.txtTalk.text = psText;
		this.txtTalk._x = -this.txtTalk.textWidth / 2;
		this.txtTalk._y = -(this.txtTalk.textHeight + 60);
		
		if ( this.oActor.isLookingLeft() ) {
			this.txtTalk._xscale = -100;
			this.txtTalk._x = -this.txtTalk._x;
		}
		else if ( this.oActor.isLookingRight() )
			this.txtTalk._xscale = 100;
	}
 }	